package com.youfang.sixthmedia.ui.searcher;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.youfang.sixthmedia.R;
import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.message.GethousesRequest;
import com.youfang.sixthmedia.message.HousefilterResponse.Area;
import com.youfang.sixthmedia.message.HousefilterResponse.HouseType;
import com.youfang.sixthmedia.message.HousefilterResponse.Price;
import com.youfang.sixthmedia.ui.BaseBuildingActivity;

public class SearcherResultActivity extends BaseBuildingActivity {
	@Override
	protected void onCreate(Bundle arg0) {
		this.setContentView(R.layout.searcher_result_activity);
		super.onCreate(arg0);

		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.area_container:
					gotoAreaActivity();
					break;
				case R.id.housetype_container:
					gotoHTActivity();
					break;
				case R.id.price_container:
					gotoPriceActivity();
					break;

				}

			}
		};

		for (int id : new int[] { R.id.area_container,
				R.id.housetype_container, R.id.price_container }) {
			this.findViewById(id).setOnClickListener(listener);
		}

		this.updateUI();

	}

	private void updateUI() {

		((TextView) this.findViewById(R.id.area))
				.setText(YoufangContext.searcher_curArea.cityid == 0 ? "区域不限"
						: YoufangContext.searcher_curArea.cityname);
		((TextView) this.findViewById(R.id.ht))

		.setText(YoufangContext.searcher_curHT.housetypeid == 0 ? "类型不限"
				: YoufangContext.searcher_curHT.housetypename);
		((TextView) this.findViewById(R.id.price))
				.setText(YoufangContext.searcher_curPrice.pricerangeid == 0 ? "价格不限"
						: YoufangContext.searcher_curPrice.pricedesc);

	}

	private static final int AREA_REQ_CODE = 2;
	private static final int HT_REQ_CODE = 3;
	private static final int PRICE_REQ_CODE = 4;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != Activity.RESULT_OK) {
			return;
		}

		switch (requestCode) {

		case AREA_REQ_CODE:

			Area newArea = (Area) data.getSerializableExtra("data");
			Area oldArea = YoufangContext.searcher_curArea;
			YoufangContext.searcher_curArea = newArea;
			if (oldArea.cityid != newArea.cityid) {
				updateUI();
				// search filter changed,we need to refresh the datalist...
				this.remoteRefresh();
			}

			break;
		case HT_REQ_CODE:
			HouseType newht = (HouseType) data.getSerializableExtra("data");
			HouseType oldht = YoufangContext.searcher_curHT;
			YoufangContext.searcher_curHT = newht;
			if (newht.housetypeid != oldht.housetypeid) {
				updateUI();
				// search filter changed,we need to refresh the datalist...
				this.remoteRefresh();
			}

			updateUI();
			break;
		case PRICE_REQ_CODE:
			Price newprice = (Price) data.getSerializableExtra("data");
			Price oldPrice = YoufangContext.searcher_curPrice;
			YoufangContext.searcher_curPrice = newprice;
			if (newprice.pricerangeid != oldPrice.pricerangeid) {
				updateUI();
				// search filter changed,we need to refresh the datalist...
				this.remoteRefresh();
			}

			break;

		}

	}

	private void gotoAreaActivity() {
		Intent intent = new Intent();

		intent.setClass(this, SearcherAreaActivity.class);
		this.startActivityForResult(intent, AREA_REQ_CODE);

	}

	private void gotoHTActivity() {
		Intent intent = new Intent();
		intent.setClass(this, SearcherHTActivity.class);
		this.startActivityForResult(intent, HT_REQ_CODE);

	}

	private void gotoPriceActivity() {
		Intent intent = new Intent();
		intent.setClass(this, SearcherPriceActivity.class);
		this.startActivityForResult(intent, PRICE_REQ_CODE);

	}

	@Override
	protected GethousesRequest makeRequest() {

		// city id will be set in the base building activity
		GethousesRequest req = new GethousesRequest();
		req.setAreaid(YoufangContext.searcher_curArea.cityid);
		req.setPropertyid(YoufangContext.searcher_curHT.housetypeid);
		req.setHouserange(YoufangContext.searcher_curPrice.pricerangeid);
		req.setKeyword(YoufangContext.searcher_keyword);
		return req;
	}

}
